package com.jewelry.web.intercepter;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.log.LogManager;
import com.jewelry.common.util.Constants;
import com.jewelry.service.IOperateLogService;
import com.jewelry.vo.OperateLogVO;
import com.jewelry.vo.UserVO;
import com.jewelry.web.vo.UserSession;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class OperateLogIntercepter extends AbstractInterceptor {

	private static final long serialVersionUID = -3432709971129712169L;

	private static Logger logger = LoggerFactory.getLogger(OperateLogIntercepter.class);
	
	private IOperateLogService operateLogService;

	public IOperateLogService getOperateLogService() {
		return operateLogService;
	}

	public void setOperateLogService(IOperateLogService operateLogService) {
		this.operateLogService = operateLogService;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String remoteAddr = request.getRemoteAddr();
		String reqeustURI = request.getRequestURI().toString();
		// 需要处理POST和GET两种情况
		String parameter = getQueryString(request);
		String userAgent = request.getHeader("User-Agent").toLowerCase();

		HttpSession session = request.getSession();
		UserSession userSession = (UserSession) session
				.getAttribute(Constants.USER_INFO);

		OperateLogVO logVO = new OperateLogVO();

		if (userSession != null) {
			UserVO user = userSession.getUserVO();
			if (user != null) {
				logVO.setUserid(user.getUserid());
			}
		}

		// 记录数据库
		logVO.setReqeustIP(remoteAddr);
		logVO.setRequestString(parameter);
		logVO.setRequestURI(reqeustURI);
		logVO.setUserAgent(userAgent);

		
		//TODO 临时屏蔽
		if(!logger.isDebugEnabled()){
			LogManager.vistLog(logVO.toString());
			operateLogService.addOperateLog(logVO);
		}

		return invocation.invoke();
	}

	private static String getQueryString(HttpServletRequest request) {
		boolean first = true;
		StringBuilder strbuf = new StringBuilder();
		Enumeration<String> emParams = request.getParameterNames();
		// do-while
		do {
			if (!emParams.hasMoreElements()) {
				break;
			}
			String sParam = emParams.nextElement();
			String[] sValues = request.getParameterValues(sParam);

			String sValue = "";
			for (int i = 0; i < sValues.length; i++) {
				sValue = sValues[i];
				if (sValue != null && sValue.trim().length() != 0
						&& first == true) {
					// 第一个参数
					first = false;
					strbuf.append(sParam).append("=").append(sValue);
				} else if (sValue != null && sValue.trim().length() != 0
						&& first == false) {
					strbuf.append("&").append(sParam).append("=")
							.append(sValue);
				}
			}
		} while (true);

		return strbuf.toString();
	}

}
